package com.yang.account.dao;

import com.yang.springcloud.pojo.ShoppingCartGoods;
import com.yang.springcloud.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class AccountImp implements AccountDao{

    @Autowired
    DataSource dataSource;

    Connection conn;
    PreparedStatement pstmt;

    @Override
    public Map<String, Object> updateBalance(List<ShoppingCartGoods> goodsList, int userid) {
        ResultSet rs;
        int i;
        int sum = 0;

        for(ShoppingCartGoods g:goodsList){
            sum += g.getGoodPrice()*g.getGoodQuantity();

            addAccountGood(userid, g);
        }

        User u = new User();
        try {
            conn = dataSource.getConnection();
            pstmt = conn.prepareStatement("SELECT * FROM users where user_id = ?");
            pstmt.setInt(1, userid);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("user_id");
                u.setUserId(id);
                int balance = rs.getInt("account_balance");
                u.setAccountBalance(balance);
                int integral = rs.getInt("user_integral");
                u.setAccountBalance(integral);
            }

            pstmt = conn.prepareStatement("update student set account_balance=?, user_integral=? where user_id=?");
            pstmt.setInt(1, u.getAccountBalance()-sum);
            pstmt.setInt(2, u.getUserIntegral()+sum/10);
            i = pstmt.executeUpdate();

            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "更新账户余额成功");
        msg.put("code", 200);
        return msg;
    }

    @Override
    public Map<String, Object> addAccountGood(int userid, ShoppingCartGoods good) {
        int i;
        try {
            conn = dataSource.getConnection();
            pstmt = conn.prepareStatement("insert into student (user_id,good_id,good_name,good_price,good_quantity,good_remaining_quantity,good_used_quantity,good_explain) values(?,?,?,?,?,?,?,?)");
            pstmt.setInt(1,userid);
            pstmt.setInt(2, good.getGoodId());
            pstmt.setString(3, good.getGoodName());
            pstmt.setInt(4, good.getGoodPrice());
            pstmt.setInt(5, good.getGoodQuantity());
            pstmt.setInt(6, good.getGoodQuantity());
            pstmt.setInt(7, 0);
            pstmt.setString(8, good.getGoodExplain());
            i = pstmt.executeUpdate();
            System.out.println(i);

            pstmt.close();
            conn.close();

        }catch (Exception e){
            e.printStackTrace();
        }
        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "插入用户商品成功");
        msg.put("code", 200);
        return msg;
    }
}
